import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JFileChooser;


public class ImportUser implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		JFileChooser choose = new JFileChooser(System.getProperty("user.home")+"\\EasyFTP\\Saved");
		javax.swing.filechooser.FileFilter filter = new javax.swing.filechooser.FileFilter(){

			@Override
			public boolean accept(File f) {
				if(f.isDirectory()) return true;
				if(f.getName().toLowerCase().endsWith(".txt")) return true;
				return false;
			}

			@Override
			public String getDescription() {
				return null;
			}
			
		};
		choose.addChoosableFileFilter(filter);
		int value = choose.showOpenDialog((Component) e.getSource());
		if(value == JFileChooser.CANCEL_OPTION)
			choose.setVisible(false);
		if(value == JFileChooser.APPROVE_OPTION){
			File file = choose.getSelectedFile();
			FileInputStream stream;
			try {
				stream = new FileInputStream(file);
				DataInputStream input = new DataInputStream(stream);
				BufferedReader read = new BufferedReader(new InputStreamReader(input));
				try {
					String user = read.readLine();
					String pass = read.readLine();
					LoginPrompt prompt = new LoginPrompt();
					char[] c = new char[pass.length()];
					for(int i = 0; i < pass.length(); i++)
						c[i] = pass.charAt(i);
					prompt.loginAttempt(user, c);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		}
		
	}
}
